package cn.itcast.demo;

import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.apache.lucene.document.Field.Store;

/**
 * @author 虎哥
 */
public class LuceneUpdateTest {

    private IndexWriter writer;


    @Before
    public void init() throws IOException {
        // 2.创建索引输出流
        // 2.1.目录
        Directory directory = FSDirectory.open(new File("C:\\lesson\\heima78\\indexDir"));
        // 2.2.配置
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
//        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        // 2.3.创建流对象
        writer = new IndexWriter(directory, conf);
    }

    @After
    public void close() throws IOException {
        // 4.提交
        writer.commit();
        writer.close();
    }

    @Test
    public void testCreateIndex() throws IOException {
        // 1.创建文档
        Document document = new Document();
        // 除了StoredField以外的，一定会创建倒排索引，但不一定存储在文档列表
        document.add(new StringField("id", "1", Store.YES));
        document.add(new TextField("title", "谷歌地图之父跳槽 Facebook，加入了传智播客！简直屌爆了啊！", Store.YES));
        // StoredField字段内容不会创建倒排索引，只存储在文档列表，用来展示
        document.add(new StoredField("image", "http://www.jd.com/1.jpg"));

        // 3.添加文档
        writer.addDocument(document);
    }

    // 批量创建索引
    @Test
    public void testCreate2() throws Exception{
        // 创建文档的集合
        List<Document> docs = new ArrayList<>();
        // 创建文档对象
        Document document1 = new Document();
        document1.add(new StringField("id", "1", Store.YES));
        document1.add(new TextField("title", "谷歌地图之父跳槽facebook", Store.YES));
        docs.add(document1);
        // 创建文档对象
        Document document2 = new Document();
        document2.add(new StringField("id", "2", Store.YES));
        document2.add(new TextField("title", "谷歌地图之父加盟FaceBook", Store.YES));
        docs.add(document2);
        // 创建文档对象
        Document document3 = new Document();
        document3.add(new StringField("id", "3", Field.Store.YES));
        document3.add(new TextField("title", "谷歌地图创始人拉斯离开谷歌加盟Facebook", Field.Store.YES));
        docs.add(document3);
        // 创建文档对象
        Document document4 = new Document();
        document4.add(new StringField("id", "4", Field.Store.YES));
        document4.add(new TextField("title", "谷歌地图之父跳槽Facebook与Wave项目取消有关", Field.Store.YES));
        docs.add(document4);
        // 创建文档对象
        Document document5 = new Document();
        document5.add(new StringField("id", "5", Field.Store.YES));
        document5.add(new TextField("title", "谷歌地图之父拉斯加盟社交网站Facebook", Field.Store.YES));
        docs.add(document5);


        // 把文档集合交给IndexWriter
        writer.addDocuments(docs);
    }

    @Test
    public void testCreate3() throws Exception{
        // 创建文档的集合
        List<Document> docs = new ArrayList<>();
        // 创建文档对象
        Document document1 = new Document();
        document1.add(new LongField("id", 1L, Store.YES));
        document1.add(new TextField("title", "谷歌地图之父跳槽facebook", Store.YES));
        docs.add(document1);
        // 创建文档对象
        Document document2 = new Document();
        document2.add(new LongField("id", 2L, Store.YES));
        document2.add(new TextField("title", "谷歌地图之父加盟FaceBook", Store.YES));
        docs.add(document2);
        // 创建文档对象
        Document document3 = new Document();
        document3.add(new LongField("id", 3L, Field.Store.YES));
        document3.add(new TextField("title", "谷歌地图创始人拉斯离开谷歌加盟Facebook", Field.Store.YES));
        docs.add(document3);
        // 创建文档对象
        Document document4 = new Document();
        document4.add(new LongField("id", 4L, Field.Store.YES));
        document4.add(new TextField("title", "谷歌地图之父跳槽Facebook与Wave项目取消有关", Field.Store.YES));
        docs.add(document4);
        // 创建文档对象
        Document document5 = new Document();
        document5.add(new LongField("id", 5L, Field.Store.YES));
        document5.add(new TextField("title", "谷歌地图之父拉斯加盟社交网站Facebook", Field.Store.YES));
        docs.add(document5);


        // 把文档集合交给IndexWriter
        writer.addDocuments(docs);
    }

    @Test
    public void testUpdate() throws IOException {
        // 准备一条文档，用来覆盖旧的文档
        Document doc = new Document();
        doc.add(new StringField("id", "1", Store.YES));
        doc.add(new TextField("title", "谷歌地图之父跳槽facebook，加入传智播客！", Store.YES));

        // 更新文档，先删除Term匹配到的文档，然后再新增一条文档
        writer.updateDocument(new Term("id", "1"), doc);
    }

    @Test
    public void testDelete() throws IOException {
        writer.deleteDocuments(new Term("id", "2"));

        writer.deleteAll();
    }
}
